class @AdminResponseListingView extends ListingView
    
    el: $('#content')

    initialize: ->
        super
        @model.bind("change", @render)
        @model.fetch()

    render: ->
        el = $(@el)

        @Navigate(@model.url(), false, true)

        @GetTemplate(SiteMap.Admin.Responses.ListingTemplate, (compiledTemplate) =>
            el.html(compiledTemplate(@model.toJSON()))
            $('#Filter_UserEmail').autocomplete(
                source: SiteMap.Admin.Shared.GetUsers
                minLength: 2
                select:(event, ui) =>
                    filter = @model.get("Filter")
                    filter.UserId = ui.item.id
                    filter.UserEmail = ui.item.value
                    @model.set("Filter", filter, { silent: true} )
            )
            $('#Filter_BookTitle').autocomplete(
                source: SiteMap.Admin.Shared.GetBooks
                minLength: 2
                select:(event, ui) =>
                    filter = @model.get("Filter")
                    filter.BookId = ui.item.id
                    filter.BookTitle = ui.item.value
                    @model.set("Filter", filter, { silent: true} )
            )
            $("#Filter_DateFrom").datepicker()
            $("#Filter_DateTo").datepicker()   
        )

    events:
        "click .search": "search"
        "click .pagination a": "pagination"
        "click th>a": "sort"
        "click .buttons>.create": "create"
        "click .buttons>.delete": "remove"
        "click tbody tr>td:not(.chk)": "edit"
        "change .page-size": "changeSize"
        "click .reset": "reset"

    search: (event) ->
        event.preventDefault()
        Loading.Start()
        Messages.WriteMessage('Apply search.')

        filter = @model.get("Filter")
        filter.DateFrom = $("#Filter_DateFrom").val()
        filter.DateTo = $("#Filter_DateTo").val()
        if $('#Filter_BookTitle').val() == ''
            filter.BookId = 0
            filter.BookTitle = ''
        if $('#Filter_UserEmail').val() == ''
            filter.UserId = 0
            filter.UserEmail = ''

        @model.set("Filter", filter, { silent: true })
        @model.set("Page", 1, { silent: true })
        @model.fetch()

    reset: (event) ->
        event.preventDefault()
        @model.set(
            "Filter",
            {
                UserId: 0
                BookId: 0
                UserEmail: ''
                BookTitle: ''
                DateFrom: ''
                DateTo: ''
            },
            { silent: true }
        )
        @model.set("Page", 1, { silent: true })
        Loading.Start()
        @model.fetch()

    remove: (event) ->
        event.preventDefault()
        form = $(@el).find("form.listing")

        if form.find("input:checked").length
           commentDeleteConfirm = new ResponseDeleteConfirm(@)
           commentDeleteConfirm.Show("Are you sure?")
        else
             Messages.WriteMessage('Choose one or more comments.')

     create: (event) ->
        event.preventDefault()
        Loading.Start()
        @Navigate(SiteMap.Admin.Responses.Create, true)

class ResponseDeleteConfirm extends ConfirmDialog
    @view: null
    constructor: (view) ->
        @view = view
    Ok: ->
            form = $(@view.el).find("form.listing")
            data = form.serialize()
            Loading.Start()
            Messages.WriteMessage('Deleting responses.')

            $.post(SiteMap.Admin.Responses.Delete, data, (response) =>
                if (response.SuccessMessage != null && response.SuccessMessage.length > 0)
                    Messages.WriteSuccessMessage(response.SuccessMessage)
                    @view.model.fetch()
                else 
                    Messages.WriteErrors(response.Errors)
                    Loading.Stop()
            )